package com.scaffold.authority.action;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.security.http.HttpLoginModule;

import com.scaffold.BasicBizAction;
import com.scaffold.authority.entity.Authority;
import com.scaffold.authority.entity.Role;
import com.scaffold.authority.entity.User;
import com.scaffold.authority.service.RoleService;
import com.scaffold.authority.service.UserService;
import com.scaffold.commons.cache.Cache;


@Controller
public class PassportJsonAction extends BasicBizAction{
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private Cache cache;
	
	@RequestMapping("/passport/login.json")
	public String login(String username, String password, String captchaId, String captcha, String rememberMe) {
		Map<String,String> result = new HashMap<String,String>();
		int times = getLoginTimes(username, true);
		if(times>10){
			result.put("msg", "登录失败超过10次，24小时内不能再次登录！");
			return this.getSuccessResult(result);
		}
		User user = this.checkLogin(username, password, captchaId, captcha);
		if(user!=null){
			this.setUserInfo(user);
			result.put("ok", "Y");
			this.clearLoginTimes(username);
		}else{
			result.put("msg", "用户名或密码错误！");
		}
		
		return this.getSuccessResult(result);
	}
	
	private void clearLoginTimes(String username){
		String timesKey = "LOGINTIMES_" + username;
		this.cache.del(timesKey);
	}
	
	private Integer getLoginTimes(String username, boolean increase){
		String timesKey = "LOGINTIMES_" + username;
		Integer times = (Integer)this.cache.get(timesKey);
		if(times==null){
			times = 0;
		}
		
		if(!increase){
			return times;
		}

		times++;
		this.cache.put(timesKey, times, 60*60*24);
		
		return times;
	}
	
	private User checkLogin(String username, String password, String captchaId, String captcha){
		User user = this.userService.findByName(username);
		if(password!=null && user!=null && password.equals(user.getPassword())){
			return user;
		}
		return null;
	}
	
	private void setUserInfo(User user){
		//role authority
		List<Role> roleList = userService.getRoleList(user.getId());
		for(Role role : roleList){
			List<Authority> auList = roleService.getAuthorityList(role.getId());
			role.setAuthorities(new LinkedHashSet<Authority>(auList));
		}
		user.setRoles(roleList);
		
		//user authority
		List<Authority> authorityList = userService.getAuthorityList(user.getId());
		user.setAuths(authorityList);
		
		this.getRequest().getSession().setAttribute(HttpLoginModule.USER_SESSION_KEY, user);
	}
	
	@RequestMapping("/passport/logout.json")
	public String logout() {
		this.getRequest().getSession().removeAttribute(HttpLoginModule.USER_SESSION_KEY);
		return this.getSuccessResult();
	}
	
}
